package com.wuqi.dev.ipcdemo;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Risky57 on 2017/5/9.
 */

public class MessengerService extends Service {

    private MsgHandler mHandler;
    private Messenger mMessenger;

    @Override
    public void onCreate () {
        super.onCreate();
        mHandler = new MsgHandler();
        mMessenger = new Messenger(mHandler);
    }

    @Nullable
    @Override
    public IBinder onBind (Intent intent) {
        return mMessenger.getBinder();
    }

    public class MsgHandler extends Handler{

        @Override
        public void handleMessage (Message msg) {
            switch (msg.what) {
                case 1:
                    msg.getData().setClassLoader(Person.class.getClassLoader());
                    Messenger messenger = msg.replyTo;
//                    messenger.send();

                    int i = msg.arg1;
                    Log.d("MsgHandler", "i:" + i);
                    Bundle data = msg.getData();
                    Log.d("MsgHandler", data.getString("key"));

//                    Log.d("MsgHandler", "msg.obj:" + msg.obj);
                    UserBean user = data.getParcelable("user");
                    Log.d("MsgHandler", "user:" + user);
                    break;
            }


            super.handleMessage(msg);
        }
    }

}
